package com.rxy.demo.service;

import com.github.pagehelper.PageHelper;
import com.rxy.demo.mapper.GoodsMapper;
import com.rxy.demo.mapper.GoodsProviderRealMapper;
import com.rxy.demo.mapper.ProviderMapper;
import com.rxy.demo.pojo.DO.Goods;
import com.rxy.demo.pojo.GoodsProviderBean;
import com.rxy.demo.pojo.DTO.GoodsProviderReal;
import com.rxy.demo.pojo.DO.Provider;
import com.rxy.demo.pojo.VO.GoodsProviderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service

public class CrudService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ProviderMapper providerMapper;

    @Autowired
    private GoodsProviderRealMapper goodsProviderRealMapper;

    public String insertGoodsProvider(GoodsProviderVO goodsProviderVO){

        String goodsName=goodsProviderVO.getGoodsName();
        Double goodsPrice=goodsProviderVO.getGoodsPrice();
        String providerName=goodsProviderVO.getProviderName();
        String providerAddress=goodsProviderVO.getProviderAddress();

        Goods goods=new Goods();
        goods.setName(goodsName);
        goods.setPrice(goodsPrice);
        goods.setCreateTime(new Date());
        goods.setStatus(0);
        goodsMapper.insert(goods);

        Provider provider=new Provider();
        provider.setProviderName(providerName);
        provider.setProviderAddress(providerAddress);
        providerMapper.insert(provider);

        GoodsProviderReal goodsProviderReal=new GoodsProviderReal();
        goodsProviderReal.setGoodsId(goods.getId());
        goodsProviderReal.setProviderId(provider.getId());
        goodsProviderRealMapper.insert(goodsProviderReal);

        return "商品新增完成";
    }

    public Object select(){
        List<Goods> goodsList= goodsMapper.selectAll() ;
        List<GoodsProviderBean> goodsProviderBeanList=new ArrayList<>();

        for (Goods goods : goodsList) {
            GoodsProviderBean goodsProviderBean =new GoodsProviderBean();
            goodsProviderBean.setGoods(goods);
            int provider_id=goodsProviderRealMapper.selectByPrimaryKey(goods.getId()).getProviderId();
            goodsProviderBean.setProvider(providerMapper.selectByPrimaryKey(provider_id));
            goodsProviderBeanList.add(goodsProviderBean);
        }
        return goodsProviderBeanList;
    }

    public String delete(Integer id){
        goodsMapper.deleteByPrimaryKey(id);
        List<GoodsProviderReal> goodsProviderRealList=goodsProviderRealMapper.selectAll();
        GoodsProviderReal goodsProviderReal1=new GoodsProviderReal();
        for (GoodsProviderReal goodsProviderReal : goodsProviderRealList) {
            if (goodsProviderReal.getGoodsId().equals(id)){
                goodsProviderReal1=goodsProviderReal;
                break;
            }
        }
        goodsProviderRealMapper.delete(goodsProviderReal1);
        return "删除成功";
    }

    public String updateGoods(GoodsProviderVO goodsProviderVO) {
        Goods goods =new Goods();
        goods.setId(goodsProviderVO.getGoodsId());
        goods.setName(goodsProviderVO.getGoodsName());
        goods.setPrice(goodsProviderVO.getGoodsPrice());
        goodsMapper.updateByPrimaryKey(goods);

        Provider provider=new Provider();
        provider.setId(goodsProviderVO.getGoodsId());
        provider.setProviderName(goodsProviderVO.getProviderName());
        provider.setProviderAddress(goodsProviderVO.getProviderAddress());
        providerMapper.updateByPrimaryKey(provider);

        return "修改成功";
    }

    public List<Goods> pageFind(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAll();
    }
}
